import re
from collections import defaultdict

text = """Hello world!  
This is an example.  
Word count is fun.  
Is it fun to count words?  
Yes, it is fun!"""

def wordcount(text):
    text = text.lower()

    #\b \b确保我们选取的是完整的单词而不是单词的一部分，\w+何字母数字字符（包括下划线）,
    words = re.findall(r'\b\w+\b', text)  # ['hello', 'world', 'this']

    # 初始化默认值类型为 int 类型
    word_counts = defaultdict(int)
    for word in words:
        word_counts[word] += 1

    return dict(word_counts)

result = wordcount(text)
print(result)







